package com.ss.android.video;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MediaPlayerActivity extends bj {
    View a;
    View b;
    boolean c;
    boolean d;
    private TextView k;
    private TextView l;
    private SeekBar m;
    private TextView n;
    private TextView o;
    private SeekBar p;
    private View q;
    private View r;
    private View s;
    private View t;
    private long u;
    private long v;
    private int w;
    private long x;
    private long y;
    private OnSeekBarChangeListener z;

    public MediaPlayerActivity() {
        this.c = true;
        this.d = false;
        this.u = 0;
        this.v = 0;
        this.w = 0;
        this.x = 0;
        this.y = 0;
        this.z = new OnSeekBarChangeListener() {
            private boolean a = false;

            public final void onProgressChanged(SeekBar arg5, int arg6, boolean arg7) {
                if (this.a) {
                    MediaPlayerActivity.this.a(((long) arg6));
                }
            }

            public final void onStartTrackingTouch(SeekBar arg2) {
                this.a = true;
            }

            public final void onStopTrackingTouch(SeekBar arg2) {
                this.a = false;
            }
        };
    }

    private static String b(long arg6) {
        long v0 = arg6 / 1000;
        int v2 = ((int) (v0 / 60));
        int v3 = ((int) (v0 % 60));
        String v1 = v0 > 9 ? String.valueOf(v2) : "0" + v2;
        String v0_1 = v3 > 9 ? String.valueOf(v3) : "0" + v3;
        return v1 + ":" + v0_1;
    }

    protected final SurfaceView a() {
        return (SurfaceView) this.findViewById(com.ss.android.article.browser.R.id.player_surface);
    }

    protected final void a(boolean arg5) {
        int v3 = View.GONE;
        if (arg5) {
            this.q.setVisibility(View.VISIBLE);
            this.r.setVisibility(v3);
            this.c = true;
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            this.q.setVisibility(v3);
            this.r.setVisibility(View.VISIBLE);
            this.c = false;
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        super.a(arg5);
    }

    public void handleMsg(Message message) {
        if (this.isViewValid()) {
            super.handleMsg(message);
            switch (message.what) {
                case 104: {
                    if (!this.d) {
                        long v0 = this.f();
                        long v2 = this.e();
                        this.k.setText(MediaPlayerActivity.b(v0));
                        this.n.setText(MediaPlayerActivity.b(v0));
                        this.l.setText("-" + MediaPlayerActivity.b(v0 - v2));
                        this.o.setText("-" + MediaPlayerActivity.b(v0 - v2));
                        this.m.setMax(((int) v0));
                        this.m.setProgress(((int) v2));
                        this.p.setMax(((int) v0));
                        this.p.setProgress(((int) v2));
                        this.f.sendMessageDelayed(this.f.obtainMessage(104), 1000);
                    }
                    return;
                }
                case 105: {
                    this.s.setVisibility(View.GONE);
                    return;
                }
            }
        }
    }

    public void onBackPressed() {
        this.finish();
    }

    public void onCompletion(MediaPlayer arg7) {
        this.d = true;
        this.a.setBackgroundResource(com.ss.android.article.browser.R.drawable.media_play_icon);
        long v0 = this.f();
        this.m.setMax(((int) v0));
        this.p.setMax(((int) v0));
        this.m.setProgress(0);
        this.p.setProgress(0);
        this.f.removeMessages(105);
        if (this.g) {
            this.s.setVisibility(View.VISIBLE);
        }
        this.f.sendEmptyMessageDelayed(105, 4000);
    }

    public void onCreate(Bundle savedInstanceState) {
        int v1;
        long v8 = 0;
        int v4 = R.string.pr;
        int v6 = R.drawable.close_popup_textpage;
        super.onCreate(savedInstanceState);
        AppData.getInstance();
        Intent v3 = this.getIntent();
        if (v3 == null) {
            UIUtils.showImageToast(((Context) this), v6, v4);
            this.finish();
            return;
        }

        this.j = v3.getStringExtra("play_url");
        try {
            v1 = Integer.parseInt(v3.getStringExtra("play_json"));
        } catch (Exception v0) {
            v1 = 0;
        }

        if (android.support.design.a.isTextEmpty(this.j)) {
            UIUtils.showImageToast(((Context) this), v6, v4);
            this.finish();
            return;
        }

        this.u = v3.getLongExtra("group_id", v8);
        this.v = v3.getLongExtra("item_id", v8);
        this.w = v3.getIntExtra("aggr_type", 0);
        this.setContentView(com.ss.android.article.browser.R.layout.media_player_activity);
        this.k = (TextView) this.findViewById(R.id.player_half_sum_length);
        this.l = (TextView) this.findViewById(R.id.player_half_left_length);
        this.m = (SeekBar) this.findViewById(R.id.player_half_seekbar);
        this.n = (TextView) this.findViewById(R.id.player_full_sum_length);
        this.o = (TextView) this.findViewById(R.id.player_full_left_length);
        this.p = (SeekBar) this.findViewById(R.id.player_full_seekbar);
        this.m.setOnSeekBarChangeListener(this.z);
        this.p.setOnSeekBarChangeListener(this.z);
        this.a = this.findViewById(com.ss.android.article.browser.R.id.player_player_btn);
        this.a.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View arg4) {
                int v2 = R.drawable.media_pause_icon;
                if (MediaPlayerActivity.this.d) {
                    MediaPlayerActivity.this.d = false;
                    MediaPlayerActivity.this.f.sendEmptyMessage(104);
                    MediaPlayerActivity.this.c();
                    MediaPlayerActivity.this.a.setBackgroundResource(v2);
                } else if (MediaPlayerActivity.this.g()) {
                    MediaPlayerActivity.this.d();
                    MediaPlayerActivity.this.a.setBackgroundResource(com.ss.android.article.browser.R.drawable.media_play_icon);
                } else {
                    MediaPlayerActivity.this.c();
                    MediaPlayerActivity.this.a.setBackgroundResource(v2);
                }
            }
        });
        this.b = this.findViewById(com.ss.android.article.browser.R.id.player_full_btn);
        this.b.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View v) {
                View v1 = MediaPlayerActivity.this.b;
                int v0 = MediaPlayerActivity.this.c ? R.drawable.media_full_player_icon : R.drawable.enlarge_video;
                AppData.isNightMode();
                v1.setBackgroundResource(v0);
                MediaPlayerActivity mediaPlayerActivity = MediaPlayerActivity.this;
                boolean v0_1 = !MediaPlayerActivity.this.c;
                mediaPlayerActivity.a(v0_1);
            }
        });
        this.q = this.findViewById(com.ss.android.article.browser.R.id.player_half_layout);
        this.r = this.findViewById(com.ss.android.article.browser.R.id.player_full_layout);
        this.s = this.findViewById(com.ss.android.article.browser.R.id.player_show);
        this.t = this.findViewById(com.ss.android.article.browser.R.id.player_progress);
        this.findViewById(com.ss.android.article.browser.R.id.player_back_btn).setOnClickListener(new View.OnClickListener() {

            public final void onClick(View view) {
                MediaPlayerActivity.this.onBackPressed();
            }
        });
        this.b();
        if (!NetworkUtils.isNetworkAvailable(((Context) this))) {
            UIUtils.showImageToast(((Context) this), v6, R.string.net_weak);
            this.finish();
            return;
        }

        if (v1 == 1) {
            new com.ss.android.video.bj.b(((bj) this), this.j).start();
            return;
        }

        String v1_1 = v3.getStringExtra("referer_url");
        String v2 = v3.getStringExtra("user_agent");
        HashMap v0_1 = null;
        if (!android.support.design.a.isTextEmpty(v1_1) || !android.support.design.a.isTextEmpty(v2)) {
            v0_1 = new HashMap();
            if (!android.support.design.a.isTextEmpty(v1_1)) {
                v0_1.put("Referer", v1_1);
            }

            if (android.support.design.a.isTextEmpty(v2)) {
            } else {
                v0_1.put("User-Agent", v2);
            }
        }

        new com.ss.android.video.bj.c(this.f, this.j, v0_1).start();
    }

    public boolean onError(MediaPlayer arg4, int arg5, int arg6) {
        String v2 = null;
        boolean v0 = false;
        if ((this.isViewValid()) && !this.isFinishing()) {
            this.g = false;
            switch (arg5) {
                case 100: {
                    if (this.e != null) {
                        this.e.release();
                        this.e = null;
                    }
                    this.b();
                    break;
                }
            }

            this.a(103, v2);
            v0 = true;
        }

        return v0;
    }

    public void onPause() {
        long v10 = 0;
        super.onPause();
        if (this.y > v10 && this.u > v10) {
            long v0 = System.currentTimeMillis() - this.y;
            if (v0 > v10) {
                this.x = v0 + this.x;
            }
        }

        if ((this.isFinishing()) && this.u > v10 && this.x > v10) {
            JSONObject v8 = new JSONObject();
            try {
                v8.put("item_id", this.v);
                v8.put("aggr_type", this.w);
            } catch (JSONException v0_1) {
            }

            MobClickCombiner.onEvent(this, "video", "play_done", this.u, this.x, v8);
        }

        this.y = v10;
    }

    public void onPrepared(MediaPlayer arg3) {
        super.onPrepared(arg3);
        arg3.start();
        this.t.setVisibility(View.GONE);
    }

    public void onResume() {
        super.onResume();
        this.f.sendEmptyMessage(104);
        this.y = System.currentTimeMillis();
    }

    public boolean onTouchEvent(MotionEvent arg6) {
        int v4 = 105;
        int v2 = View.GONE;
        if (arg6.getAction() == 1 && this.t.getVisibility() == v2 && (this.g)) {
            this.f.removeMessages(v4);
            if (this.s.getVisibility() == v2) {
                this.s.setVisibility(View.VISIBLE);
                this.f.sendEmptyMessageDelayed(v4, 4000);
            } else {
                this.s.setVisibility(v2);
            }
        }

        return super.onTouchEvent(arg6);
    }
}

